package com.sunrise.visual.frame.common
{
	import com.adobe.serialization.json.JSON;
	import com.adobe.serialization.json.JSONParseError;
	import com.adobe.utils.StringUtil;
	import com.sunrise.visual.frame.error.ErrorManager;
	import com.sunrise.visual.frame.layout.StateBarManager;
	
	import mx.controls.Alert;
	import mx.core.Application;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	import mx.utils.URLUtil;

	public class ServerConnector
	{
		public function ServerConnector(sqlId:String = null,actionUrl:String = null, dbType:int = 0, paramname:Object = null)
		{
			this.sqlId = sqlId;
			this.dbType = dbType;
			this.actionUrl = actionUrl;
			server =  new HTTPService();
			server.resultFormat = HTTPService.RESULT_FORMAT_TEXT;
			server.addEventListener(ResultEvent.RESULT, resultHandler);
			server.addEventListener(FaultEvent.FAULT,faultHandler);
		}
		
		public var sqlId:String;
		public var actionUrl:String;
		public var dbType:int;
		public var paramname:Object;
		
		private function resultHandler(e:ResultEvent):void {
			try{
				if(e.result != null) {
					var result:Object = JSON.decode(e.result.toString());
					if(sqlId != null && result.hasOwnProperty(sqlId)) {
						var list:Array = result[sqlId];
						if(list != null && success != null) 
							success.call(null, list,sqlId);
					}
					else if(sqlId != null){
						if(multiSqlSuccess != null)
							multiSqlSuccess.call(null, result);
					}
				}
			} catch (error:JSONParseError) {
				ErrorManager.addError(new Error(e.result.toString()), this);
				callFailure(e.result.toString());
			} catch(error:Error) {
				ErrorManager.addError(error, this);
				callFailure(error.message);
			}
		}
		
		private function callFailure(message:String):void {
			if(failure!= null) {
				failure.call(null, message);
			}
		}
		
		private function faultHandler(e:FaultEvent):void {
			ErrorManager.addError(e.fault, this);
			callFailure(e.message == null ? "error" : e.message.toString());
		}
		
		public function send():void {
			var param:String = JSON.encode(paramname);
			var obj:Object = new Object();
			obj.dbtype = dbType||"0";
			obj.sqlId = sqlId ||"";
			obj.paramname = param || "";
			server.url = serverUrl + actionUrl;
//			StateBarManager.instance.run();
			server.send(obj);
		}
		
		private var server:HTTPService = null;
		
		public static var serverUrl:String = "http://localhost:8080/csgbi/";
		public var success:Function = null;
		public var failure:Function = null;
		public var multiSqlSuccess:Function = null;
		
		public static function initConnection():void {
			var url:String = Application.application.url;
			var appcfg:XML = Application.application.appCfg;
			
			if(appcfg != null && appcfg.hasOwnProperty("connect") 
				&& appcfg.connect[0].hasOwnProperty("url") 
				&& StringUtil.stringHasValue(appcfg.connect[0].url[0].toString()))  {
				ServerConnector.serverUrl = appcfg.connect[0].url[0].toString();
			} else if(url!= null && url.length > 0 && URLUtil.isHttpURL(url)) {
				var serverNameWithPort:String = URLUtil.getServerNameWithPort(url);
				var start:int = url.indexOf("/", url.indexOf(serverNameWithPort)) + 1;
				var length:int = url.indexOf("/", start) + 1;
				ServerConnector.serverUrl = url.substring(0, length);
			}
		}
	}
}